import request from '../utils/request.js';
import * as actionTypes from './actionTypes.js';
import { message } from 'antd';
export default function httpRequest(params) {
      const IP_URL = process.env.REMOTE_SERVER_IP + params.api

      return (dispatch) => {
            request("POST", IP_URL, {
                  cmd: params.cmd,
                  type: "request",
                  request: {
                        ...params.request
                  }
            }).then(response => {
                  const result = response.response;
                  if (result.res) {

                        if (params.method === "loginRequest") {
                              sessionStorage.setItem(
                                    "islogin",
                                    JSON.stringify(result.res)
                              );
                        }

                        dispatch(eval(params.method + "(result)"));
                  } else {
                        message.error(result.exception, 1.5);
                  }

            }).catch(error => { console.log(error) });
      };
};

// 登录相关
const loginRequest = result => ({ type: actionTypes.LOGIN, result });

// 忘记密码相关
const forgetPasswordRequest = result => ({ type: actionTypes.FORGETPASSWORD, result });

// 注册相关
const registerRequest = result => ({ type: actionTypes.REGISTER, result });

// 验证码相关
const validateRequest = result => ({ type: actionTypes.VALIDATE, result });

//  获取短信邮箱验证码相关
const phone_emailRequest = result => { return }

// //  获取验证码或更新验证码
// export const getValidate = () => {
//       const IP_URL = process.env.REMOTE_SERVER_IP  + params.api
// }